package com.wtwd.wtwddevice.listener.deviceServerMsg.handler.impl;

import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.CmdStateEnum;
import com.wtwd.device.constant.FrequencyTypeEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.constant.SeparatorConstant;
import com.wtwd.device.model.BloodOxygen;
import com.wtwd.device.model.BloodPressure;
import com.wtwd.device.model.vo.AppFrequencyVo;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.DeviceBloodOxygen;
import com.wtwd.wtwddevice.model.entity.DeviceBloodPressure;
import com.wtwd.wtwddevice.service.IAppCommService;
import com.wtwd.wtwddevice.service.IDeviceBloodPressureService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IFrequencyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 血压频率回复
 */
@MsgType(MsgTypeEnum.W_BP_FREQUENCY)
@Component
public class SetBloodPressureFrequencyHandlerImpl implements IMsgHandler {

    @Resource
    private IDeviceService deviceService;

    @Resource
    private IDeviceBloodPressureService deviceBloodPressureService;

    @Resource
    private IFrequencyService frequencyService;

    @Resource
    private IAppCommService appCommService;

    @Override
    public void handleMsg(MessageBase messageBase) {
        WTProtocol wtProtocol = messageBase.getWtProtocol();
        String body = wtProtocol.getBody();
        String[] dataArray = body.split(SeparatorConstant.POUND_SIGN);
        Integer bloodPressure = Integer.valueOf(dataArray[0]);
        if (bloodPressure.equals(0)) return;
        String isWear =dataArray[1];
        Long deviceId = deviceService.getDeviceIdByImei(wtProtocol.getImei());
        DeviceBloodPressure deviceBloodPressure = new DeviceBloodPressure();
        deviceBloodPressure.setDeviceId(deviceId);
        deviceBloodPressure.setValue(bloodPressure);
        deviceBloodPressure.setTime(wtProtocol.getTime());
        deviceBloodPressure.setNumber(wtProtocol.getNumber());
        deviceBloodPressure.setType(messageBase.getType());
        deviceBloodPressure.setIsWear(isWear);
        deviceBloodPressure.setCreateTime(new Date().getTime());
        deviceBloodPressure.setUpdateTime(new Date().getTime());
        deviceBloodPressureService.insert(deviceBloodPressure);
        frequencyService.updateState(deviceId, FrequencyTypeEnum.W_BP_FREQUENCY.getType(), CmdStateEnum.SUCCESS.getState());
        //推送到app
        BloodPressure data = new BloodPressure();
        BeanUtils.copyProperties(deviceBloodPressure, data);
        AppFrequencyVo appFrequencyVo = new AppFrequencyVo();
        appFrequencyVo.setType(FrequencyTypeEnum.W_BP_FREQUENCY.getType());
        appFrequencyVo.setValue(data);
        AppProtocol appProtocol = AppProtocol.builder()
                .imei(wtProtocol.getImei())
                .cmdName(MsgTypeEnum.APP_SET_FREQUENCY.getCmdName())
                .data(appFrequencyVo)
                .build();
        appCommService.pushDeviceState(deviceId, appProtocol);
    }


}
